from PyQt4.QtCore import *
from PyQt4.QtGui import *

from ui.CDTAboutDialog import *
from ui.CDTDocumentWindow import *

class CDTMainWindow(QMainWindow):
    def __init__(self):
        """ CDTMainWindow constructor """
        QMainWindow.__init__(self)

        self.__initActions()
        self.__initMenu()

        # for MDI application
        self.mdi = QMdiArea()
        self.setCentralWidget(self.mdi)

        self.setWindowTitle('Coditor')
        self.statusBar().showMessage('Ready', 2000)

    def __createAction(self, text, **argd):
        action = QAction(text, self)

        checkable = argd.get('checkable', False)
        if checkable is not False:
            action.setCheckable(True)

        icon = argd.get('icon', None)
        if icon is not None:
            action.setIcon(QIcon(icon))

        shortcut = argd.get('shortcut', None)
        if shortcut is not None:
            action.setShortcut(shortcut)

        slot = argd.get('slot', None)
        if slot is not None:
            signal = argd.get('signal', 'triggered()')
            self.connect(action, SIGNAL(signal), slot)

        tip = argd.get('tip', None)
        if tip is not None:
            action.setToolTip(tip)
            action.setStatusTip(tip)

        return action

    def __initActions(self):
        self.actions = []
        
        self.newAction = self.__createAction('&New...', shortcut='Ctrl+N', slot=self.__new)
        self.actions.append(self.newAction)
        self.newDocAction = self.__createAction('New &Document', shortcut='Ctrl+Alt+N', slot=self.__newDoc)
        self.actions.append(self.newDocAction)
        self.openAction = self.__createAction('&Open...', shortcut='Ctrl+O', slot=self.__open)
        self.actions.append(self.openAction)
        self.saveAction = self.__createAction('&Save', shortcut='Ctrl+S', slot=self.__save)
        self.actions.append(self.saveAction)
        self.saveAsAction = self.__createAction('Save &As...', slot=self.__saveAs)
        self.actions.append(self.saveAsAction)
        self.saveAllAction = self.__createAction('Save &All', slot=self.__saveAll)
        self.actions.append(self.saveAllAction)
        self.exitAction = self.__createAction('E&xit', shortcut='Ctrl+Q', slot=self.__quit)
        self.actions.append(self.exitAction)
        
        self.undoAction = self.__createAction('Undo', shortcut='Ctrl+Z', slot=self.__undo)
        self.actions.append(self.undoAction)
        self.redoAction = self.__createAction('Redo', shortcut='Ctrl+Y', slot=self.__redo)
        self.actions.append(self.redoAction)
        
        self.aboutAction = self.__createAction('&About Coditor...', slot=self.__about)
        self.actions.append(self.aboutAction)
        self.newAction = self.__createAction('&New...', shortcut='Ctrl+N', slot=self.__new)
        self.actions.append(self.newAction)
        self.prefAction = self.__createAction('&Preferences...', shortcut='Ctrl+;', slot=self.__new)
        self.actions.append(self.newAction)

    def __initMenu(self):
        self.menubar = QMenuBar(self)
        self.fileMenu = QMenu('&File', self.menubar)
        self.fileMenu.addAction(self.newAction)
        self.fileMenu.addAction(self.newDocAction)
        self.fileMenu.addSeparator()
        self.fileMenu.addAction(self.openAction)
        self.fileMenu.addSeparator()
        self.fileMenu.addAction(self.saveAction)
        self.fileMenu.addAction(self.saveAsAction)
        self.fileMenu.addAction(self.saveAllAction)
        self.fileMenu.addSeparator()
        self.fileMenu.addAction(self.exitAction)
        self.editMenu = QMenu('&Edit', self.menubar)
        self.editMenu.addAction(self.undoAction)
        self.editMenu.addAction(self.redoAction)
        self.windowMenu = QMenu('&Window', self.menubar)
        self.helpMenu = QMenu('&Help', self.menubar)
        self.helpMenu.addAction(self.aboutAction)
        self.menubar.addAction(self.fileMenu.menuAction())
        self.menubar.addAction(self.editMenu.menuAction())
        self.menubar.addAction(self.windowMenu.menuAction())
        self.menubar.addAction(self.helpMenu.menuAction())
        self.setMenuBar(self.menubar)

    def __new(self):
        pass
    
    def __newDoc(self):
        doc = CDTDocumentWindow(self.mdi)
        self.mdi.addSubWindow(doc)
        if (len(self.mdi.subWindowList()) == 1):
            doc.showMaximized()
        else:
            doc.show()

    def __open(self):
        pass

    def __save(self):
        pass

    def __saveAs(self):
        pass
    
    def __saveAll(self):
        pass
    
    def __quit(self):
        QCoreApplication.instance().closeAllWindows()

    def __undo(self):
        pass

    def __redo(self):
        pass

    def __about(self):
        dlg = CDTAboutDialog(self)
        dlg.exec_()

    def __new(self):
        pass
